package com.mililoan.common.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 表示分页的一个页集，封装了一页的数据，并且包含当前是第几页，总记录条数等
 * */
public class PageResultSet<T> {
	/** 分页-键值 */
    public static final String PAGE_RESULT_KEY = "PAGE_RESULT_SET";
	/**第一页*/
	public static final String FIRST_PAGE = "firstPage";

	/**末页*/
	public static final String LAST_PAGE = "lastPage";

	/**上一页*/
	public static final String BACK_PAGE = "backPage";

	/**下一页*/
	public static final String NEXT_PAGE = "nextPage";
	
	/**当前页*/
	public static final String LOCAL_PAGE = "localPage";

	/**不代页码信息*/
	public static final String NO_PAGE = "noPage";

	public final static int PAGESIZE = 10;

	private int recordNum;

	private int curPage;

	private int startRow;

	private int rowNum;

	private int pageSize;

	private List<T> result=new ArrayList<T>();

	//参数的集合 key 参数名 value 参数值
	private Map<String,String> params;

	// 请求这个 地址
	private String servletPath;

	//page的参数名
	private String pageParaName = "page";

	private String action;
	
	public int getRecordNum() {
		return recordNum;
	}

	public void setRecordNum(int recordNum) {
		this.recordNum = recordNum;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		if (curPage < 1)
			this.curPage = 1;
		else
			this.curPage = curPage;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getRowNum() {
		return rowNum;
	}

	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public PageResultSet() {
		this.pageSize = PAGESIZE;
	}

	public PageResultSet(String p_requestPath) {
		this.pageSize = 10;
		this.servletPath = p_requestPath;
	}

	public PageResultSet(int pageS) {
		pageSize = pageS;
	}

	public int getAllPage() {
		int allPage = recordNum / pageSize;
		if (recordNum % pageSize != 0)
			allPage++;
		if (allPage == 0)
			allPage = 1;
		return allPage;
	}

	public int getEndRow() {
		int endRow = startRow + pageSize - 1;
		if (endRow > recordNum)
			endRow = recordNum;
		return endRow;
	}

	public int getNextPage() {
		if (curPage >= getAllPage())
			return getAllPage();
		else
			return curPage + 1;
	}

	public int getBackPage() {
		if (curPage <= 1)
			return 1;
		else
			return curPage - 1;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List getResult() {
		return result;
	}

	public void setResult(List result) {
		this.result = result;
	}

	/**
	 * @param p_paraName 参数名
	 * @param p_paraName 参数值
	 */
	public void addPram(String p_paraName, String p_paraValue) {

		if (params == null)
			params = new HashMap<String,String>();
		if ((p_paraValue!=null) && (p_paraValue.length()!=0))
			params.put(p_paraName, p_paraValue);
	}
	/**
	 * @param p_paraName 参数名
	 * @param p_paraName 参数值
	 */
	public void addPram(String p_paraName, Integer p_paraValue) {

		if (params == null)
			params = new HashMap<String,String>();
		if (p_paraValue!=null)
			params.put(p_paraName, p_paraValue.toString());
	}
	/**
	 * @param p_paraName 参数名
	 * @param p_paraName 参数值
	 */
	public void addPram(String p_paraName, Long p_paraValue) {

		if (params == null)
			params = new HashMap<String,String>();
		if (p_paraValue!=null)
			params.put(p_paraName, p_paraValue.toString());
	}

	/**
	 * @param string
	 */
	public void setPageParaName(String string) {
		pageParaName = string;
	}

	/**
	 * @param string
	 */
	public void setServletPath(String string) {
		servletPath = string;
	}

	/**
	 * 得到请求地址
	 * */
	private StringBuffer getPagePath() {
		StringBuffer l_result = new StringBuffer(servletPath).append("&");

		if (params != null) {
			Set l_entrySet = params.entrySet();
			Iterator l_it = l_entrySet.iterator();

			while (l_it.hasNext()) {
				Map.Entry l_entry = (Map.Entry) l_it.next();
				l_result.append(l_entry.getKey()).append("=").append(
						l_entry.getValue()).append("&");
			}
		}
		
		return l_result;
	}

	/**
	 * 得到上一页的地址
	 * */
	public String getBackPagePath() {
		StringBuffer l_pathBuffer = getPagePath();
		l_pathBuffer.append(pageParaName).append("=")
				.append(this.getBackPage());
		return l_pathBuffer.toString();
	}

	/**
	 * 得到下一页的地址
	 * */
	public String getNextPagePath() {
		StringBuffer l_pathBuffer = getPagePath();
		l_pathBuffer.append(pageParaName).append("=")
				.append(this.getNextPage());
		return l_pathBuffer.toString();
	}

	/**
	 * 得到首页的地址
	 * */
	public String getFirstPagePath() {
		StringBuffer l_pathBuffer = getPagePath();
		l_pathBuffer.append(pageParaName).append("=1");
		return l_pathBuffer.toString();
	}

	/**
	 * 得到末页的地址
	 * */
	public String getLastPagePath() {
		StringBuffer l_pathBuffer = getPagePath();
		l_pathBuffer.append(pageParaName).append("=").append(this.getAllPage());
		return l_pathBuffer.toString();
	}

	/**
	 * 得到当前页的地址
	 * */
	public String getLocalPagePath() {
		StringBuffer l_pathBuffer = getPagePath();
		l_pathBuffer.append(pageParaName).append("=").append(this.curPage);
		return l_pathBuffer.toString();
	}
	
	/**
	 * 得到无页码的地址
	 * */
	public String getNoPagePath() {
		String l_pathString = getPagePath().toString();
		l_pathString = l_pathString.substring(0, l_pathString.length() - 1);

		return l_pathString;
	}

	/**
	 * @param string
	 */
	public void setJump(String string) {
		action = string;
	}
}
